/**
 * Task 处理异步任务
 */

import fs from 'fs'
import { task } from 'folktale/concurrency/task'
import {
  split,
  find,
} from 'lodash/fp'

const readFile = filename => task(resolver => {
  fs.readFile(filename, 'utf-8', (err, data) => {
    if (err) {
      resolver.reject(err)
    }
    resolver.resolve(data)
  })
})

// readFile 返回一个Task函子

readFile('../package.json')
  .map(split('\n'))
  .map(find((x: any) => x.includes('version')))
  .run()
  .listen({
    onRejected (err) {
      console.log(err)
    },
    onResolved (value) {
      console.log(value) // "version": "1.0.0"
    }
  })
